package org.jspresso.model.extension;

import org.jspresso.framework.action.ActionBusinessException;
import org.jspresso.framework.application.backend.BackendControllerHolder;
import org.jspresso.framework.application.backend.persistence.hibernate.HibernateBackendController;
import org.jspresso.framework.model.component.AbstractComponentExtension;
import org.jspresso.framework.model.component.IComponent;
import org.jspresso.model.TestEntity;
import org.jspresso.tester.utils.TestEntitySerializer;

/**
 * Generated by Jspresso Developer Studio
 */
public class TestEntityExtension extends AbstractComponentExtension<TestEntity> {

	public TestEntityExtension(TestEntity component) {
		super(component);

		registerNotificationForwarding(component, TestEntity.VALUE, TestEntity.DISPLAYABLE);
	}

	/**
	 * generated by Jspresso Developer Studio
	 * 
	 * @return
	 */
	public Class<?> getClazz() {
		Class<?> clazz = null;
		try
		{
			clazz = Class.forName (getComponent().getClazzName());
		}
		catch (ClassNotFoundException e)
		{
			throw new ActionBusinessException(e.getMessage());
		}
		return clazz;
	}

	/**
	 * generated by Jspresso Developer Studio
	 * 
	 * @return
	 */
	public String getDisplayable() {
		TestEntity testEntity = getComponent();
		return testEntity.getClazz().getSimpleName() + " : " + testEntity.getValue().toString();
	}

	/**
	 * generated by Jspresso Developer Studio
	 * 
	 * @return
	 */
	public IComponent getValue() {
		if(getComponent().getEntityId() != null &&  getClazz() != null) {
			return TestEntitySerializer.unserializeFromStore(getComponent().getEntityId(),( Class<? extends IComponent>) getClazz(), getComponent().getTestScope(), (HibernateBackendController)BackendControllerHolder.getCurrentBackendController());
		}
		return null;
	}
	
	public void setValue(IComponent component) {
		System.out.println("Entity set : " + component.toString());
	}

}
